package com.youku.plugins.endcardV4New.view
{
   import flash.display.Sprite;
   import flash.text.TextField;
   import flash.display.DisplayObject;
   import com.youku.plugins.endcardV4New.model.data.RelativeVideoData;
   import com.youku.utils.DrawUtil;
   import com.youku.utils.AssetUtil;
   import com.youku.plugins.endcardV4New.EndCardFont;
   import flash.events.MouseEvent;
   import com.youku.utils.Tweener;
   
   public class RelativePreviewBox extends Sprite
   {
      
      public function RelativePreviewBox(param1:uint, param2:RelativeVideoData) {
         super();
         this._ID = param1;
         this._relativeVideodata = param2;
         this.initSkin();
         this.layout();
         this.addEvents();
      }
      
      private var _ID:uint;
      
      private var _width:Number;
      
      private var _height:Number;
      
      private var _panel:Sprite;
      
      private var _titleTextField:TextField;
      
      private var _imageBackground:Sprite;
      
      private var _image:DisplayObject;
      
      private var _boxInfo:RelativePreviewBoxInfo;
      
      private var _boxInfoMask:Sprite;
      
      private var _relativeVideodata:RelativeVideoData;
      
      private function initSkin() : void {
         this._width = 200;
         this._height = 110;
         this._panel = new Sprite();
         this._panel.useHandCursor = true;
         this._panel.buttonMode = true;
         this._imageBackground = DrawUtil.RectangleSprite(0,0,this._width,this._height,0,1,null,0);
         this._imageBackground.mouseEnabled = false;
         this._panel.addChild(this._imageBackground);
         this._titleTextField = AssetUtil.getTextField();
         this._titleTextField.width = 190;
         this._titleTextField.height = 48;
         this._titleTextField.defaultTextFormat = EndCardFont.FONT_VIDEO_TITLE;
         this._titleTextField.mouseEnabled = false;
         this._titleTextField.text = this._relativeVideodata.title;
         this._boxInfo = new RelativePreviewBoxInfo(this._relativeVideodata);
         this._boxInfo.visible = false;
         this._boxInfoMask = new Sprite();
         this._boxInfoMask.graphics.beginFill(0,0);
         this._boxInfoMask.graphics.drawRect(0,0,200,110);
         this._boxInfoMask.graphics.endFill();
         this._boxInfo.mask = this._boxInfoMask;
         addChild(this._boxInfoMask);
         this._panel.hitArea = this._imageBackground;
         this.hitArea = this._imageBackground;
         addChild(this._panel);
      }
      
      private function addEvents() : void {
         this._panel.addEventListener(MouseEvent.ROLL_OVER,this.onRollOver);
         this._panel.addEventListener(MouseEvent.ROLL_OUT,this.onRollOut);
      }
      
      private function removeEvents() : void {
         this._panel.removeEventListener(MouseEvent.ROLL_OVER,this.onRollOver);
         this._panel.removeEventListener(MouseEvent.ROLL_OUT,this.onRollOut);
      }
      
      private function onRollOver(param1:MouseEvent) : void {
         if(this._panel.contains(this._titleTextField))
         {
            this._panel.removeChild(this._titleTextField);
         }
         this._panel.addChild(this._boxInfo);
         Tweener.to(this._boxInfo,0.1,
            {
               "visible":true,
               "y":0
            });
      }
      
      private function onRollOut(param1:MouseEvent) : void {
         Tweener.to(this._boxInfo,0.1,
            {
               "y":this._height,
               "onComplete":this.onBoxInfoHide
            });
      }
      
      private function onBoxInfoHide() : void {
         this._boxInfo.visible = false;
         if(this._panel.contains(this._boxInfo))
         {
            this._panel.removeChild(this._boxInfo);
         }
         this._panel.addChild(this._titleTextField);
      }
      
      public function addImage(param1:DisplayObject) : void {
         this._image = param1;
         this._panel.addChild(this._image);
         this._panel.addChild(this._titleTextField);
      }
      
      public function get ID() : uint {
         return this._ID;
      }
      
      public function get link() : String {
         return this._relativeVideodata.link;
      }
      
      public function get target() : String {
         return this._relativeVideodata.target;
      }
      
      public function setWH(param1:Number, param2:Number) : void {
         this._width = param1;
         this._height = param2;
         this._imageBackground.width = this._width;
         this._imageBackground.height = this._height;
         this._boxInfoMask.width = this._width;
         this._boxInfoMask.height = this._height;
         this._boxInfo.setWH(this._width,this._height);
         this.layout();
      }
      
      private function layout() : void {
         this._titleTextField.x = 5;
         if(this._titleTextField.numLines == 1)
         {
            this._titleTextField.y = this._height - 25;
         }
         else
         {
            this._titleTextField.y = this._height - 45;
         }
      }
      
      override public function get width() : Number {
         return this._imageBackground.width;
      }
      
      override public function get height() : Number {
         return this._imageBackground.height;
      }
      
      private function uninstall() : void {
         this.removeEvents();
      }
   }
}
